package uc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import cn.onekit.android.*;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import uc.IconView;
import uc.IconInfo;

public class IconGridViewAdapter extends BaseAdapter implements DragGridView.DragGridBaseAdapter {
	  List<IconInfo> _icons = new ArrayList<IconInfo>();

	private int mHidePosition = -1;
	Context context;
	IconGridView appsGridView;
	public boolean mini = true;

	public void setData(final List<IconInfo> icons) {
		this._icons=icons;
		this.notifyDataSetChanged();
	}

	public IconGridViewAdapter(Context context, IconGridView appsGridView) {
		this.context = context;
		this.appsGridView = appsGridView;
	}

	@Override
	public int getCount() {
		return _icons.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public interface OnCoverChangeListener {
		void onCoverChange();
	}

	public void setOnCoverChangeListener(OnCoverChangeListener OnCoverChangeListener) {
		this.OnCoverChangeListener = OnCoverChangeListener;
	}

	OnCoverChangeListener OnCoverChangeListener;

	public interface OnModeChangeListener {
		void onModeChange(boolean mode);
	}

	public void setOnModeChangeListener(OnModeChangeListener OnModeChangeListener) {
		this.OnModeChangeListener = OnModeChangeListener;
	}

	OnModeChangeListener OnModeChangeListener;

	@SuppressLint({ "ViewHolder", "InflateParams" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		//
		IconView iconView = new IconView(context);
		iconView.setLayoutParams(
				new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(IconView.HEIGHT)));
		IconInfo app = _icons.get(position);
		Drawable icon = app.icon;
		String title = app.title;
		iconView.setData(icon, title);
		if (mini) {
			iconView.hideTitle(true);
		}
		if (position == mHidePosition) {
			iconView.setVisibility(View.INVISIBLE);
		}
		return iconView;
	}

	@Override
	public void reorderItems(int oldPosition, int newPosition) {
		if (oldPosition < 0 || oldPosition >= _icons.size()) {
			return;
		}
		if (newPosition < 0 || newPosition >= _icons.size()) {
			return;
		}
		IconInfo temp = _icons.get(oldPosition);
		if (oldPosition < newPosition) {
			for (int i = oldPosition; i < newPosition; i++) {
				Collections.swap(_icons, i, i + 1);
			}
		} else if (oldPosition > newPosition) {
			for (int i = oldPosition; i > newPosition; i--) {
				Collections.swap(_icons, i, i - 1);
			}
		}

		_icons.set(newPosition, temp);
	}

	@Override
	public void setHideItem(int hidePosition) {
		this.mHidePosition = hidePosition;
		notifyDataSetChanged();
	}

}
